﻿using System;
using System.Reflection;
using System.Reflection.Emit;
using Cubicle.Core.DataContracts;
using Cubicle.Excel.Config;
using Cubicle.Excel.Hosting;

namespace Cubicle.Excel.Binding
{
    public class BindingElement
    {

        #region Static

        private static object _lock = new object();

        private static int _counter = 0;
        private static int GetNextCounter()
        {
            lock (_lock)
            {
                return _counter++;
            }
        }

        #endregion

        #region Virtual

        public int Id { get; set; }
        public MethodInfo Method { get; set; }
        public MethodData RemoteMethod { get; set; }
        public ParameterData[] RemoteParameters { get; set; }
        public MethodInfo Indirection { get; set; }
        public DynamicMethod Proxy { get; set; }
        public ExcelMethodInfo Emi { get; set; }
        public string TargetHandle { get; set; }
        public object TargetObject { get; set; }
        public BaseDescription BindingDescription { get; set; }
        public EngineProxy Engine { get; set; }
        public Type ReturnType { get; set; }
        public Type[] ParameterTypes { get; set; }
        public MethodInfo Helper { get; set; }

        public int InvokeCount { get; private set; }
        public void IncrementInvokeCount()
        {
            InvokeCount++;
        }

        public BindingElement(BaseDescription bindingDescription)
        {
            Id = GetNextCounter();
            BindingDescription = bindingDescription;
        }

        #endregion

    }

}